#include "StdAfx.h"
#include "VSqlListView.h"

#include "VSqlDataView.h"
#include "VSqlMetaView.h"

// VSqlListView ///////////////////////////////////////////////////////////////

//
VSqlListView::VSqlListView(VSqlMetaModel *metaModel,QWidget *parent)
	:QFrame(parent)
	,m_metaModel(metaModel)
{
	m_dataModel = new VSqlDataModel(metaModel->metaMap(),this);
	m_dataView = new VSqlDataView(this);
		m_dataView->setModel(m_dataModel);

	m_dataInfo = new QLabel(this);
	
	////////////////////////////////////////////////
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
		mainLayout->setContentsMargins(0,0,0,0);
		mainLayout->addWidget(m_dataInfo);
		mainLayout->addWidget(m_dataView);
	setLayout(mainLayout);
}

//
VSqlListView::~VSqlListView()
{
}

//
void VSqlListView::setMeta(const VSqlMeta &meta)
{
	if (!meta.item().isItemList())
	{
		m_dataInfo->setText("");
		m_dataModel->clear();
		return;
	}

	VSqlMetaMap *metaMap = m_metaModel->metaMap();
	VSqlMeta metaParent = metaMap->meta_byIdNode(meta.node().idNodeParent().value());
	QVariantList idRecList;
	
	foreach (const VSqlMeta metaChild,metaMap->childMetaList_byMeta(metaParent))
	{
		if (metaChild.node().refIdItem().value() == meta.node().refIdItem().value())
			idRecList << metaChild.node().refIdRec().value();
	}
	
	QVariant idTable = meta.item().refIdItem().value();
	VSqlItem itemTable = metaMap->itemMap()->item_byIdItem(idTable);
	VSqlMeta metaTable = metaMap->meta_byKeyNameEx(itemTable.keyName().value().toString());

	m_dataInfo->setText(QString()
		+ "<h3><i>"
		+ itemTable.displayName().value().toString()
		+ "</i></h3>"
	);
	m_dataModel->setTable(metaTable,idRecList);
}
